<?php
/**
 * Copyright 2011  SURFfoundation
 * 
 * This file is part of ESCAPE.
 * 
 * ESCAPE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ESCAPE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ESCAPE.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * For more information:
 * http://escapesurf.wordpress.com/
 * http://www.surffoundation.nl/
 * 
 * Contact: d.vierkant@utwente.nl
 * 
 * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3
 */
?>
<?php
// test cases:
// http://chroniclingamerica.loc.gov/lccn/sn85066387/1895-03-05/ed-1/

/**
 * OAI ORE metadata extractor
 * 
 * Extracts metadata from OAI ORE resource maps discovered from HTTP or HTML META headers.
 */
class metaextractor_extractor_OaiOre extends metaextractor_Extractor
{
	protected $name = "OAI ORE";
	protected $supportedUrlPattern = null;
	
	/**
	 * (non-PHPdoc)
	 * @see escape/metaextractor/extractor/metaextractor_Extractor::processUrl()
	 */
	public function processUrl($url, $urlContentFetcher)
	{
		$results = array();
		$metadataDocuments = array();
		
		$urlContent = $urlContentFetcher->getUrlContent($url);
		
		// try to get a more accurate subject URI from the HTTP headers
		$httpHeaders = $urlContent->getHttpHeaders();
		$subjectUri = metaextractor_Util::extractPrimaryTopicUriFromHttpHeaders($httpHeaders, $url);
		
		$resourceMapUrls = array();
		$aggregationUrls = array();
		
		// check for links to resouce maps in (X)HTML content
		// only accept complete (X)HTML pages
		$mimeType = $urlContent->getMimeType();
		if($urlContent->isComplete() && ($mimeType == 'text/html' || $mimeType == 'application/xhtml+xml')) // perhaps also 'application/xml' and 'text/xml'
		{
			// fetch the text content in UTF8
			$html = $urlContent->getUtf8TextContent();
			
			// try to get a more accurate subject URI from the (X)HTML content
			if($subjectUri === null)
			{
				$subjectUri = metaextractor_Util::extractPrimaryTopicUriFromHtml($html, $url);
			}
			
			// use the URL as the subject URI if no subject URI was extracted
			if($subjectUri === null)
			{
				$subjectUri = $url;
			}
			
			// extract LINK tags
			preg_match_all('~<link [^>]+>~ims', $html, $matches);
			//print_r($matches);
			$linkTags = $matches[0];
			
			// parse LINK tags
			foreach($linkTags as $linkTag)
			{
				$rel = null;
				$type = null;
				$href = null;
				
				preg_match_all('~([-a-z]+) *= *((?:"[^"]*")|(?:\'[^\']*\'))~ims', $linkTag, $matches);
				foreach($matches[1] as $attributeIdx => $attributeName)
				{
					$attributeValue = trim(html_entity_decode(substr($matches[2][$attributeIdx], 1, -1)));

					$attributeName = strtolower($attributeName);
					if($attributeName == 'rel')
					{
						$rel = $attributeValue;
					}
					else if($attributeName == 'type')
					{
						$type = $attributeValue;
					}
					else if($attributeName == 'href')
					{
						$href = $attributeValue;
					}
				}
				
				// check for links with rel="resourcemap" and type="application/rdf+xml"
				if(strtolower($rel) === 'resourcemap' && strtolower($type) === 'application/rdf+xml' && $href !== null)
				{
					$resourceMapUrls[] = metaextractor_Util::convertRelativeToAbsoluteUrl($url, $href);
				}
				// check for links with rel="aggregation"
				else if(strtolower($rel) === 'aggregation' && $href !== null)
				{
					$aggregationUrls[] = metaextractor_Util::convertRelativeToAbsoluteUrl($url, $href);
				}
			}
		}
		
		if(count($resourceMapUrls) === 0)
		{
			// check for links to resource maps in the HTTP headers
			foreach($httpHeaders as $httpHeader)
			{
				if(preg_match('~^Link:~i', $httpHeader) && preg_match('~rel="resourcemap"~i', $httpHeader) && preg_match('~type="application/rdf\\+xml~i', $httpHeader) && preg_match('~<([^>]+)>~', $httpHeader, $urlMatches))
				{
					// resource map URL
					$resourceMapUrls[] = metaextractor_Util::convertRelativeToAbsoluteUrl($url, $urlMatches[1]);
				}
				else if(preg_match('~^Link:~i', $httpHeader) && preg_match('~rel="aggregation"~i', $httpHeader) && preg_match('~<([^>]+)>~', $httpHeader, $urlMatches))
				{
					// aggregation URL
					$aggregationUrls[] = metaextractor_Util::convertRelativeToAbsoluteUrl($url, $urlMatches[1]);
				}
			}
		}
		
		// remove duplicate resource map URI's
		$resourceMapUrls = array_unique($resourceMapUrls);

		// remove duplicate resource map URI's
		$aggregationUrls = array_unique($aggregationUrls);
		
		if(count($resourceMapUrls) === 0 && count($aggregationUrls) > 0)
		{
			// no links to resource maps found but we did get links to aggregations
			// try opening the aggregation URLs instead, we should be able to get metadata through content negotiation or cool URIs
			$resourceMapUrls = $aggregationUrls;
		}
		
		// fetch the resource maps
		foreach($resourceMapUrls as $resourceMapUrl)
		{
			try {
				$resourceMapUrlContent = $urlContentFetcher->getUrlContent($resourceMapUrl);
				// currently only RDF/XML resource maps are supported 
				if($resourceMapUrlContent->isComplete() && strtolower($resourceMapUrlContent->getMimeType()) === 'application/rdf+xml')
				{
					// parse the result
					$metadataDocuments[] = new simplerdf_RdfDocument($resourceMapUrlContent->getContent(), metaextractor_Util::getBaseUrl($url));
				}
			}
			catch (metaextractor_exception_Exception $e)
			{
				// ignore
			}
		}
		
		// create the result objects
		foreach($metadataDocuments as $metadataDocument)
		{
			$result = new metaextractor_Result();
			$result->setExtractorId($this->getId());
			$result->setExtractorName($this->getName());
			$result->setSubjectUri($subjectUri);
			$result->setRdfDocument($metadataDocument);
			
			$results[] = $result;
		}
		
		return $results;
	}
}
